package com.hanson.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.common.util.BusinessUtil;
import com.hanson.entity.AcctbkSeqBean;
import com.hanson.entity.ScoreSeqBean;
import com.hanson.entity.UserBean;
import com.hanson.entity.WeiXinNotifyBean;
import com.hanson.mapper.AcctbkSeqBeanMapper;
import com.hanson.mapper.WeiXinNotifyBeanMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AcctbkService {
    @Autowired
    private AcctbkSeqBeanMapper acctbkSeqBeanMapper;
    @Autowired
    private WeiXinNotifyBeanMapper weiXinNotifyBeanMapper;
    @Autowired
    private UserService userService;

    public  List<AcctbkSeqBean> getAcctbkInfoPageByOpenId(String openId,Integer page,Integer limit) {
        UserBean userBean = userService.selectByOpenId(openId);
        if (null != userBean) {
            Page<AcctbkSeqBean> pageInfo = new Page<>(page, limit);
            List<AcctbkSeqBean> list = acctbkSeqBeanMapper.selectPageByOpenId(pageInfo,openId);
            if(CollectionUtils.isNotEmpty(list)){
                list.stream().forEach(c->{
                    c.setMoney(BusinessUtil.getWebAmount(Integer.parseInt(c.getMoney())) + "元");
                });
            }
            return list;
        }
        return null;
    }

    public JSONObject getAcctbkInfoByOpenId(String openId) {
        UserBean userBean = userService.selectByOpenId(openId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("totalMoney", "0.0");
        jsonObject.put("totalOut", "0.0");
        jsonObject.put("totalCost", "0.0");
        jsonObject.put("canOut", "0.0");
        if (null != userBean) {
            jsonObject.put("totalMoney", BusinessUtil.getWebAmount(userBean.getMoney()));
            //用户余额大于600可提现
            if(userBean.getMoney() >= 60000){
                jsonObject.put("canOut", jsonObject.getString("totalMoney"));
            }
            List<AcctbkSeqBean> list1 = acctbkSeqBeanMapper.selectByOpenId(openId);
            if (CollectionUtils.isNotEmpty(list1)) {
                int totalOut = 0;
                for (AcctbkSeqBean acctbkSeqBean : list1) {
                    if (acctbkSeqBean.getType() == 2) {
                        totalOut += Integer.parseInt(acctbkSeqBean.getMoney());
                    }
                }
                jsonObject.put("totalOut", BusinessUtil.getWebAmount(totalOut));
            }

            List<WeiXinNotifyBean> list2 = weiXinNotifyBeanMapper.selectByOpenId(openId);
            if (CollectionUtils.isNotEmpty(list2)) {
                int totalCost = 0;
                for (WeiXinNotifyBean weiXinNotifyBean : list2) {
                    totalCost += weiXinNotifyBean.getTotalFee();
                }
                jsonObject.put("totalCost", BusinessUtil.getWebAmount(totalCost));
            }
        }
        return jsonObject;
    }
}
